﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Reflection;
namespace 玩具商城管理系统.znew
{
    public partial class th : Form
    {
        public th()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProudct;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select orderT_id,orderT_cid,orderT_check,employee_id,order_Note,orderT_th,orderT_img from orderT";
            daProudct = new SqlDataAdapter(str, DB.cn);
            daProudct.Fill(ds, "order_info");
        }
        void showAll()
        {
            DB.GetCn();
            string str = "select orderT_id,orderT_cid,orderT_check,employee_id,order_Note,orderT_th,orderT_img from orderT";
            SqlCommand cmd = new SqlCommand(str, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = dgvth.Rows.Add();
                dgvth.Rows[index].Cells[0].Value = rdr[0];
                dgvth.Rows[index].Cells[1].Value = rdr[1];
                dgvth.Rows[index].Cells[2].Value = rdr[2];
                dgvth.Rows[index].Cells[3].Value = rdr[3];
                dgvth.Rows[index].Cells[4].Value = rdr[4];
                dgvth.Rows[index].Cells[5].Value = rdr[5];
                try
                {
                    Image imageColumn = Image.FromFile(Application.StartupPath + rdr[6]);
                    dgvth.Rows[index].Cells["Column7"].Value = imageColumn;
                }
                catch
                {
                    Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    dgvth.Rows[index].Cells["Column7"].Value = imgeColumn;
                }
            }
            rdr.Close();
            DB.cn.Close();

        }
        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvth.Columns.Add(acCode);
            int columnIndex = dgvth.Columns["acCode"].Index;
            dgvth.Columns[columnIndex].Width = 80;
            acCode.DisplayIndex = 0;

        }
        

        private void th_Load(object sender, EventArgs e)
        {
            dgvth.RowTemplate.Height = 50;
            showXz();
            
            if (login.Dflag == false)
            {
                MessageBox.Show("需要以销售部员工身份登录才可以进入");
                this.Close();
            }
            else
            {
                    this.dgvth.RowTemplate.Height = 50;
                showAll();
                    this.dgvth.Columns[0].HeaderText = "订单编号";
                    this.dgvth.Columns[1].HeaderText = "用户名称";
                    this.dgvth.Columns[2].HeaderText = "审核状态";
                    this.dgvth.Columns[3].HeaderText = "审核人员";
                    this.dgvth.Columns[4].HeaderText = "用户备注";
                    this.dgvth.Columns[5].HeaderText = "退货理由";
                    this.dgvth.Columns[6].HeaderText = "退货图片";
                }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DB.GetCn();
            int s = dgvth.Rows.Count;
            for (int i = 0; i < dgvth.Rows.Count; i++)
            {//更新客户订单表hot
                if (dgvth.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    DB.GetCn();
                    string result = dgvth.Rows[i].Cells["Column1"].Value.ToString();
                    string ok = "update orderT set orderT_check= '已退货' where orderT_id='"+result+"'";
                    DB.sqlEx(ok); 
                    DB.cn.Close();
                    DB.GetCn();
                    string ok3 = "update hot set hot_people= '已退货' where orderT_id='" + result + "'";
                    DB.sqlEx(ok3);
                    DB.cn.Close();
                    if (string.IsNullOrEmpty(dgvth.Rows[i].Cells["Column4"].Value?.ToString()))
                    {
                        DB.GetCn();
                        dgvth.Rows[i].Cells["Column4"].Value = login.username;
                        string ok2 = "update orderT set employee_id= '"+login.username+"' where orderT_id='" + result + "'";
                        DB.sqlEx(ok2);
                    }
                    MessageBox.Show("审核成功");
                }
            }
            DB.cn.Close();
            dgvth.Rows.Clear();
            showAll();
        }

        private void dgvth_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvth.Rows.Count > 0)
            {
                for (int i = 0; i < dgvth.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvth.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
